package com.atguigu.cart.vo;

import com.atguigu.common.tool.utils.Func;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author : wenchao.long
 * @date : Created in 2021/8/14 15:25 周六
 * @description :
 */
@Data
@ApiModel("购物车")
public class CartVO {

    @ApiModelProperty("购物项")
    List<CartItemVO> items;

    @ApiModelProperty("商品数量")
    private Integer countNum;

    @ApiModelProperty("商品类型数量")
    private Integer countType;

    @ApiModelProperty("商品总价")
    private BigDecimal totalAmount;

    @ApiModelProperty("商品减价")
    private BigDecimal reduce = new BigDecimal("0");

    public Integer getCountNum() {
        int count = 0;
        if (Func.isNotEmpty(items)) {
            count = items.stream().mapToInt(CartItemVO::getCount).sum();
        }
        return count;
    }

    public Integer getCountType() {
        int type = 0;
        if (Func.isNotEmpty(items)) {
            type = items.size();
        }
        return type;
    }

    public BigDecimal getTotalAmount() {
        BigDecimal amount = new BigDecimal("0");
        if (Func.isNotEmpty(items)) {
            for (CartItemVO item : items) {
                if (item.getCheck()) {
                    BigDecimal totalPrice = item.getTotalPrice();
                    amount = amount.add(totalPrice);
                }
            }
        }
        return amount.subtract(getReduce());
    }
}
